package backend;

import backend.rss.ItemsContainer;
import backend.rss.KulerRSSFeed;
import backend.rss.KulerRSSFeed.ParseType;
import backend.rss.items.DetailedThemeItem;

public class Controller
{
	private static final int DEFAULT_ITEMS_PER_PAGE = 10;
	
	private int itemsPerPage = DEFAULT_ITEMS_PER_PAGE;
	
	public Controller()
	{
	}
	
	public Controller(int itemsPerPage)
	{
		this.itemsPerPage = itemsPerPage; 
	}

	public int getItemsPerPage()
	{
		return itemsPerPage;
	}
	
	public ItemsContainer getThemeComments(String themeId)
	{
		return getThemeComments(themeId, -1);
	}
	
	public ItemsContainer getThemeComments(String themeId, int start)
	{
		//ItemsContainer items = getComments(buildThemeCommentsQuery(themeId, start));
		//System.out.println(items.getCommentItems());
		//return items;
		getDetailedTheme(themeId);
		return null;
	}
	
	public ItemsContainer getUserComments(String email)
	{
		return getUserComments(email);
	}
	
	public ItemsContainer getUserComments(String email, int start)
	{
		return getComments(buildUserCommentsQuery(email, start));
	}
	
	public ItemsContainer defaultSearch(String search)
	{
		return defaultSearch(search, -1);
	}
	
	public ItemsContainer searchByTag(String tag)
	{
		return defaultSearch("tag:"+tag, -1);
	}

	public ItemsContainer searchByTag(String tag, int start)
	{
		return defaultSearch("tag:"+tag, start);
	}
	
	public ItemsContainer defaultSearch(String search, int start)
	{
		ItemsContainer items = getThemes(buildSearchQuery(search, start));
		return items;
	}
	
	public ItemsContainer getRecentPopularThemes()
	{
		return getRecentPopularThemes(-1);
	}
	
	public ItemsContainer getRecentPopularThemes(int start)
	{
		ItemsContainer items = getThemes(buildQuery("popular", start, "&timespan=30"));
		return items;				
	}
	
	public ItemsContainer getHighestRatedThemes()
	{
		return getHighestRatedThemes(-1);
	}
	
	public ItemsContainer getHighestRatedThemes(int start)
	{
		ItemsContainer items = getThemes(buildQuery("rating", start));
		return items;		
	}

	public ItemsContainer getRecentThemes()
	{
		return getRecentThemes(-1);
	}
	
	public ItemsContainer getRecentThemes(int start)
	{
		ItemsContainer items = getThemes(buildQuery("recent", start));
		return items;		
	}

	public ItemsContainer getRandomThemes()
	{
		return getRandomThemes(-1);
	}

	public ItemsContainer getRandomThemes(int start)
	{
		ItemsContainer items = getThemes(buildQuery("random", start));				 						 
		return items;		
	}
	
	public DetailedThemeItem getDetailedTheme(String themeId)
	{
		StringBuffer sb = new StringBuffer("http://kuler.adobe.com/kuler/services/theme/get.cfm?");
		
		sb.append("themeID="+themeId);
		System.out.println(sb);
		
		KulerRSSFeed kulerFeed 	= new KulerRSSFeed(sb.toString(), ParseType.DETAILED_THEME);
		DetailedThemeItem item	= kulerFeed.parseDetailedTheme();
		return item;
	}
	
	private String buildSearchQuery(String search, int start)
	{
		StringBuffer sb = new StringBuffer("http://kuler.adobe.com/kuler/services/search/get.cfm?");
		
		sb.append("searchQuery="+search);
		sb.append("&itemsPerPage="+itemsPerPage);
		
		if (start != -1) sb.append("&startIndex="+start);
		
		return sb.toString();
	}
	
	private String buildThemeCommentsQuery(String themeId, int start)
	{
		return buildCommentsQuery("themeID", themeId, start);
	}
	
	private String buildUserCommentsQuery(String userEmail, int start)
	{
		return buildCommentsQuery("email", userEmail, start);
	}
	
	private String buildCommentsQuery(String field, String search, int start)
	{
		StringBuffer sb = new StringBuffer("http://kuler.adobe.com/kuler/services/comments.cfm?");
		
		sb.append("itemsPerPage="+itemsPerPage);
		sb.append("&"+field+"="+search);
		
		if (start != -1) sb.append("&startIndex="+start);
		
		System.out.println(sb);
		
		return sb.toString();
	}
	
	private String buildQuery(String type, int start)
	{
		return buildQuery(type, start, null);
	}

	/**
	 * 
	 * @param type
	 * @param start
	 * @param optionalFields caller will need to add in appropriate delimiters (ie &)
	 * @return
	 */
	private String buildQuery(String type, int start, String optionalFields)
	{
		StringBuffer sb = new StringBuffer("http://kuler.adobe.com/kuler/services/theme/getList.cfm?");
		
		sb.append("listtype="+type);
		sb.append("&itemsPerPage="+itemsPerPage);
		
		if (optionalFields != null) sb.append(optionalFields);
		
		if (start != -1) sb.append("&startIndex="+start);
		
		System.out.println("Query:"+sb);
		
		return sb.toString();
	}
	
	private ItemsContainer getComments(String url)
	{
		return parseFeed(url, ParseType.COMMENT);
	}
	
	private ItemsContainer getThemes(String url)
	{
		return parseFeed(url, ParseType.THEME);
	}
	
	public ItemsContainer parseFeed(String url)
	{
		return parseFeed(url, ParseType.THEME);
	}
	
	public ItemsContainer parseFeed(String url, ParseType parseType)
	{
		KulerRSSFeed kulerFeed = new KulerRSSFeed(url, parseType);
		return kulerFeed.getItemsInFeed();
	}

}
